package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.domain.Place.Zone;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.interfaces.services.IEventService;
import es.ats.codington.festival.utils.FERSDataConnection;

/**
 * 
 * EventService
 *
 */
public class EventService implements IEventService {

	Connection connection = null;
	
	//---------------------- Methods ------------------------------
	
	/**
	 * ShowEvents
	 * 
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public ArrayList<Event> getShowEvents() throws SQLException, ClassNotFoundException {
		ArrayList<Event> showEvents = new ArrayList<Event>();
		try {
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			showEvents = dao.showEvents();
		} catch (ClassNotFoundException exception) {
			System.out.println(exception.getMessage());
		} catch (SQLException exception) {
			System.out.println(exception.getMessage());
		}
		connection.close();
		return showEvents;
	}
	
	/**
	 * RegisterNewEvent
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int registerNewEvent(Event event) throws SQLException, ClassNotFoundException{	
		int affectedRows = 0;
		try{
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			affectedRows = dao.registerNewEvent(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		connection.close();
		return affectedRows;
		}


	/**
	 * DeleteEvent
	 * 
	 * @param idEvent
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int deleteEvent(int idEvent) throws SQLException, ClassNotFoundException{
		int affectedRows = 0;
		try{
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			affectedRows = dao.deleteEvent(idEvent);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		connection.close();
		return affectedRows;
	}

	/**
	 * Update Event
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int updateEvent(Event event) throws SQLException, ClassNotFoundException{
		int affectedRows = 0;
		try{
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			affectedRows = dao.updateEvent(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		connection.close();
		return affectedRows;
	}

	/**
	 * CheckEvent
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEvent(Event event) throws SQLException{
		boolean bool=false;
		try{
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			bool=dao.checkEventDAO(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		connection.close();
		return bool;
	}
	
	/**
	 * GetIdEvent
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public int getIdEvent(Event event) throws SQLException{
		int idEvent=0;
		try{
			connection = FERSDataConnection.getConnection();
			EventDAOImpl dao = new EventDAOImpl(connection);
			idEvent=dao.getIdEventDAO(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		connection.close();
		return idEvent;
	}
	
	

public Event getEvent(Event e) throws SQLException{
	
	
	try {
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);  
		e=dao.getEventDAO(e);
					
	}catch (Exception ee) {
		System.out.println("getvisitorService: "+ ee.getMessage());
	} 

		connection.close();


	return e;
}

public ArrayList<Event> getShowEventsByPlace(Event event) throws SQLException, ClassNotFoundException {
	ArrayList<Event> showEvents = new ArrayList<Event>();
	try {
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		showEvents = dao.showEventsByPlaceDAO(event);
	} catch (ClassNotFoundException exception) {
		System.out.println(exception.getMessage());
	} catch (SQLException exception) {
		System.out.println(exception.getMessage());
	}
	connection.close();
	return showEvents;
}



public Stadium showStadium(Stadium stadium) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		stadium= dao.showStadiumDAO(stadium);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return stadium;
}

public Theater showTheater(Theater theater) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		theater= dao.showTheaterDAO(theater);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return theater;
}

public Museum showMuseum(Museum museum) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		museum= dao.showMuseumDAO(museum);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return museum;
}

public LargeBusiness showLargeBusiness(LargeBusiness largeBusiness) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		largeBusiness= dao.showLargeBusinessDAO(largeBusiness);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return largeBusiness;
}

public Zoo showZoo(Zoo zoo) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		zoo= dao.showZooDAO(zoo);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return zoo;
}

public Park showPark(Park park) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		park= dao.showParkDAO(park);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return park;
}
public TraditionalMarket showTraditionalMarket(TraditionalMarket traditionalMarket) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		traditionalMarket= dao.showTraditionalMarketDAO(traditionalMarket);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return traditionalMarket;
}

public TouristAttraction showTouristAttraction(TouristAttraction touristAttraction) throws ClassNotFoundException, SQLException {
	
	try{
		connection = FERSDataConnection.getConnection();
		EventDAOImpl dao = new EventDAOImpl(connection);
		touristAttraction= dao.showTouristAttractionDAO(touristAttraction);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	connection.close();
	return touristAttraction;
}



}
	
	
